from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pathlib import Path
from fastapi.middleware.cors import CORSMiddleware
from config import *
from router_epub_split import router_module
from fastapi import APIRouter

# 创建应用
app = FastAPI(title=app_name)
# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有源，生产环境建议指定具体域名
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有头信息
)

# 配置静态文件
static_dir = Path("static")
static_dir.mkdir(parents=True, exist_ok=True)
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")

router_main = APIRouter()

# 定义根路径路由
@router_main.get("/")
def root():
    return {"message": f"欢迎访问{app_name}，API文档请访问 /docs"}

# 注册路由
app.include_router(router_main)
app.include_router(router_module, prefix=router_dict["router_module"])


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="127.0.0.1", port=app_port, reload=True)
